# -*- coding: UTF-8 -*- #

import os
import sys
if __name__ != "__main__":
    path = os.path.dirname( __file__ )
    sys.path.append( path )
    os.chdir( path )

import tornado
import tornado.web
import tornado.options
import tornado.autoreload
import tornado.httpserver
from tools.Base import BaseHandler
from tornado.options import define, options

define( "port", default = 8888,
        help = "run on the given port", type = int )

settings = dict( 
            blog_title = u"Blogis",
            template_path = os.path.join( os.path.dirname( __file__ ), "templates" ),
            static_path = os.path.join( os.path.dirname( __file__ ), "static" ),
            xsrf_cookies = True,
            cookie_secret = "11oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
            login_url = "/auth/login",
            autoescape = None,
            session_age = 7 * 24 * 60 * 60,
        )

class MainHandler( BaseHandler ):
    def get( self ):
        values = dict( 
            paging = None,
        )

        self.Render( 'index.html',
                    **values
                    )
from tools.Captcha import Captcha
from tools.Users import Regisration, Avt
from Modules.Blog import BlogIndex, BlogNewPost, BlogPostEdit, BlogPostView, BlogNewPostComment, BlogTagsSearch
Handlers = [
    ( r"/", MainHandler ),

    ( r"/captcha", Captcha ),
    ( r'/registration', Regisration ),
    ( r'/avt', Avt ),

    ( r'/blog', BlogIndex ),
    ( r'/blog/newpost', BlogNewPost ),
    ( r'/blog/edit/([^/]+)', BlogPostEdit ),
    ( r'/blog/postview/([^/]+)', BlogPostView ),
    ( r'/blog/newpostcomment/([^/]+)', BlogNewPostComment ),
    ( r'/blog/search/tags', BlogTagsSearch ),
    ]

if __name__ == "__main__":
    tornado.locale.load_gettext_translations(os.path.join(os.path.dirname(__file__), "translations"),"blogis")
    
    class Application( tornado.web.Application ):
        def __init__( self ):
            handlers = Handlers
            tornado.web.Application.__init__( self, handlers, **settings )

    #tornado.options.parse_config_file("server.conf")
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer( Application() )
    http_server.listen( options.port )
    tornado.autoreload.watch('templates')
    tornado.autoreload.watch('sysdir')
    tornado.autoreload.start( io_loop = None, check_time = 500 )
    tornado.ioloop.IOLoop.instance().start()

    '''http_server = tornado.httpserver.HTTPServer(Application())
    http_server.bind(options.port)
    http_server.start(0) # Forks multiple sub-processes
    tornado.autoreload.start(io_loop=None, check_time=500)
    tornado.ioloop.IOLoop.instance().start()'''
